﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Waker.Common.Cards
{
    public class CardHelper
    {
        #region 基础数据
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_init", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_init();
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_buzzer", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_buzzer(int control, byte opentm, byte closetm, int repcnt);
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_exit", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_exit();
        /// <summary>
        /// 该函数使射频电路关闭所规定的时间若 msec=0射频电路部分将一直处于关闭状态一	直到下一个Request命令到来关闭射频能使天线内的所有卡复位
        /// </summary>
        /// <param name="msec">射频电路关闭时间以毫秒为单位</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_reset", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_reset(byte msec);
        /// <summary>
        /// 检查在有效范围内是否有卡的存在 0表示成功否则返回错误码
        /// </summary>
        /// <param name="mode">请求类型 	mode=0：请求天线范围内 IDLE状态的卡 HALT状态的除外 	mode=1：请求天线范围内的所有卡 </param>
        /// <param name="tagtype">返回的目标类型</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_request", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_request(byte mode, out byte tagtype);
        /// <summary>
        /// 此函数开始防冲突操作
        /// </summary>
        /// <param name="bcnt">为预选卡所分配的位的个数通常Bcnt=0</param>
        /// <param name="snr">返回卡的序号</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_anticoll", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_anticoll(byte bcnt, out UInt32 snr);
        /// <summary>
        /// 此函数选择某一个序号的卡 0表示成功否则返回错误码
        /// </summary>
        /// <param name="snr">卡的序号</param>
        /// <param name="size">卡的容量</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_select", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_select(UInt32 snr, out byte size);
        /// <summary>
        /// 在对卡进行读写加减等操作前必须对卡进行验证若卡中的密钥与所传输的密码相匹	配 0表示成功否则返回错误码 
        /// </summary>
        /// <param name="mode">密钥类型,密钥 A：0x00,密钥 B：0x04 </param>
        /// <param name="key">存储密钥缓冲区</param>
        /// <param name="secnr">要验证的卡扇区号</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_authkey", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_authkey(byte mode, byte[] key, byte secnr);
        /// <summary>
        /// 是对指定的块进行整块写操作 0表示成功否则返回错误码
        /// </summary>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="data">块数据长度 16个字节函数功能此</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_write", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_write(byte addr, byte[] data);
        /// <summary>
        /// 此函数往一个块中写入数
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_writeval", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_writeval(byte addr, int value);
        /// <summary>
        /// 此函数功能是对一个块进行值操作并把结果存到 trans_addr中
        /// </summary>
        /// <param name="mode">模式 1字节加 C1H减 C0H复制 C2H </param>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="value">要操作的值 </param>
        /// <param name="trans_addr">传输块地址</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_value", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_value(byte mode, byte addr, int value, byte trans_addr);
        /// <summary>
        /// 是读取卡的一个块的数据
        /// </summary>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="data">块数据长度 16个字节函数功能此</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_read", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_read(byte addr, byte[] data);
        /// <summary>
        /// 此函数读出一个块中的数值
        /// </summary>
        /// <param name="addr">块号(0x00--0x3f)</param>
        /// <param name="value">存储要读出的值</param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_readval", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_readval(byte addr, out int value);
        /// <summary>
        /// 此函数使被选择的卡产生一个暂停即使之处于 Halt模式处于 Halt模式的卡只能用 	ALL方式进行选	择即 RC500USB_request函数中的 mode参数值设为 1
        /// </summary>
        /// <param name="addr"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_halt", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_halt();
        /// <summary>
        /// 模块每次上电复位之后都必须首先调用此函数对模块进行初始化才能进行进一步的	操作
        /// </summary>
        /// <returns></returns>
        [DllImport(@"CardsAPI\RC500USB.dll", EntryPoint = "RC500USB_config", CharSet = CharSet.Ansi)]
        public static extern byte RC500USB_config();
        #endregion
    }
}
